package com.my.ads.lib.live.JobSchedule;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

import com.my.ads.lib.live.service.JobSchedulerService;

import static android.content.Context.JOB_SCHEDULER_SERVICE;

/**
 * Created by xuanyang on 2018/3/12.
 */

public class JobScheduleTools {


    private final static int WAKE_REQUEST_CODE = 18086;
    private final static int intervalTime = 60 * 1000;
    private final static String STA_WAKE_ACTION = "com.clean.alarm";

    public static void openJobSchedule(Context context) {
        try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                JobScheduler jobScheduler = (JobScheduler) context.getSystemService(JOB_SCHEDULER_SERVICE);
                JobInfo jobInfo = new JobInfo.Builder(1, new ComponentName(context.getPackageName(), JobSchedulerService.class.getName()))
                        .setPeriodic(intervalTime)
                        .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
                        .build();
                jobScheduler.schedule(jobInfo);
            } else {
                AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
                Intent alarmIntent = new Intent();
                alarmIntent.setAction(STA_WAKE_ACTION);
                PendingIntent operation = PendingIntent.getBroadcast(context, WAKE_REQUEST_CODE, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
                alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), intervalTime, operation);

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
